#include "DiamondGame.h"

namespace King {
	DiamondGame::DiamondGame()
	: mEngine("./assets")
	, mLastFrameMouseDown(false)
	, mSelectedDiamondIndex(-1, -1)
	{
		srand (time(NULL));
		int col = GameManager::getInstance().getColMax(); 
		int row = GameManager::getInstance().getRowMax(); 
		for (int i = 0; i < col; i++)
		{
			for (int j = 0; j < row; j++)
			{
				DiamondIndex index;
				index.col = i;
				index.row = j;
				Diamond diamond(index);
				mDiamondMap.insert(std::pair<DiamondIndex, Diamond>(index, diamond));
			}
		}

		mMouseState = kNull;
		mMouseClickStartPoint.x = 0.0f;
		mMouseClickStartPoint.y = 0.0f;

		mMouseCurrentPoint.x = 0.0f;
		mMouseCurrentPoint.y = 0.0f;
	}

	DiamondGame::~DiamondGame()
	{
	}

	void DiamondGame::Start()
	{
		mEngine.Start(*this);
	}

	void DiamondGame::Update()
	{
		if (mEngine.GetMouseButtonDown())
		{
			if (mLastFrameMouseDown == false)
			{
				mMouseState = kClickStart;
				mMouseClickStartPoint.x = mEngine.GetMouseX();
				mMouseClickStartPoint.y = mEngine.GetMouseY();
				mLastFrameMouseDown = true;
			}
			else
			{
				mMouseState = kDragging;
			}
			mMouseCurrentPoint.x = mEngine.GetMouseX();
			mMouseCurrentPoint.y = mEngine.GetMouseY();
		}
		else
		{
			if (mLastFrameMouseDown == true)
			{
				mMouseState = kClickEnd;
				mLastFrameMouseDown = false;
			}
		}

		if (mMouseState == kClickStart)
		{
			DiamondIndex selected;
			std::map<DiamondIndex, Diamond>::iterator iter = mDiamondMap.begin();
			for (; iter != mDiamondMap.end(); ++iter)
			{
				Diamond& diamond = iter->second;
				if (diamond.containsPoint(mMouseClickStartPoint))
				{
					selected = iter->first;
					break;
				}
			}

			if (selected.isNeighborhood(mSelectedDiamondIndex))
			{
				Swap(selected, mSelectedDiamondIndex);
				mSelectedDiamondIndex = DiamondIndex::NULLINDEX;
			}
			else
			{
				mSelectedDiamondIndex = selected;
//				printf("mSelectedDiamondIndex col=%d, row=%d", mSelectedDiamondIndex.col, mSelectedDiamondIndex.row);
			}
		}
		
		Render();
	}

	void DiamondGame::Render()
	{
		std::map<DiamondIndex, Diamond>::const_iterator iter = mDiamondMap.begin();
		for (; iter != mDiamondMap.end(); ++iter)
		{
			const Diamond& diamond = iter->second;
			DiamondPoint position = diamond.getPosition();
			Engine::Texture color = diamond.getColor();
			if (mSelectedDiamondIndex != DiamondIndex::NULLINDEX)
			{
				if (mSelectedDiamondIndex == iter->first)
				{
					mEngine.Render(Engine::Texture::TEXTURE_WHITE, position.x, position.y);
//					printf("Render White posiontx = %f, positionY= %f", position.x, position.y);
				}
			}
			mEngine.Render(color, position.x, position.y);
		}
	}

	void DiamondGame::Swap(const DiamondIndex& first, const DiamondIndex& second)
	{
		std::map<DiamondIndex, Diamond>::iterator iter = mDiamondMap.find(first);
		assert(iter != mDiamondMap.end());
		if (iter == mDiamondMap.end())
		{
			return;
		}

		iter = mDiamondMap.find(second);
		assert(iter != mDiamondMap.end());
		if (iter == mDiamondMap.end())
		{
			return;
		}

		Diamond temp = mDiamondMap[first];
		mDiamondMap[first] = mDiamondMap[second];
		mDiamondMap[first].setIndex(first);
		mDiamondMap[second] = temp;
		mDiamondMap[second].setIndex(second);
	}
}

